/*
 * TestFreeSpaceManager.cpp
 *
 */

#include "TestFreeSpaceManager.h"
#include <iostream>
#include <stdlib.h>
#include <assert.h>

TestFreeSpaceManager::TestFreeSpaceManager() {
	fileName = "testFS.txt";
}

TestFreeSpaceManager::~TestFreeSpaceManager() {
}

void TestFreeSpaceManager::runTests() {
	string comando("rm ");
	comando+=fileName;
	system(comando.c_str());
	system("rm test2.txt.fs");
	try {
		testBurstPop();
		testBurstPush();
		testBurstPop();
		testRandomPush();
		testBlockUse();
	}
	catch(...) {
		std::cout << "Test Fail!";
	}


}

void TestFreeSpaceManager::testBurstPop() {
	std::cout << "Test BurstPop" << std::endl;
	std::cout << "-------------" << std::endl;
	FreeSpaceManager fsManager(fileName);
	int popQuantity=1000;
	Offset offsetFromPop=0;
	for(int i=0; i < popQuantity; i++) {
		offsetFromPop = fsManager.pop();
		assert(i==offsetFromPop);
	}

	assert((popQuantity-1)==offsetFromPop);
	assert(popQuantity==fsManager.getUseBlockCount());
	std::cout << "Cantidad usada: " << fsManager.getUseBlockCount() << std::endl;
	std::cout << std::endl;
}

void TestFreeSpaceManager::testBurstPush() {
	std::cout << "Test BurstPush" << std::endl;
	std::cout << "--------------" << std::endl;

	FreeSpaceManager fsManager(fileName);

	std::cout << "Cantidad usada: " << fsManager.getUseBlockCount() << std::endl;
	std::cout<< "Liberando todos los bloques"<<endl;
	Offset offSetQuantity=fsManager.getUseBlockCount();
	for(int i=0; i < offSetQuantity; i++) {
		fsManager.push(i);
	}

	std::cout << "VER ARCHIVO: " << fileName << std::endl;
	assert(0==fsManager.getUseBlockCount());
	std::cout << "Cantidad usada: " << fsManager.getUseBlockCount() << std::endl;
	std::cout << std::endl;
}

void TestFreeSpaceManager::testRandomPush() {
	std::cout << "Test RandomPush" << std::endl;
	std::cout << "---------------" << std::endl;
	FreeSpaceManager fsManager(fileName);

	for(int i=0; i < 1000; i+=3) {
		fsManager.push(i);
	}

	std::cout << "VER ARCHIVO: " << fileName << std::endl;
	std::cout << "Cantidad usada: " << fsManager.getUseBlockCount() << std::endl;
	std::cout << std::endl;
}

void TestFreeSpaceManager::testBlockUse() {
	std::cout << "Test BlockUse" << std::endl;
	std::cout << "---------------" << std::endl;

	FreeSpaceManager fsManager("test2.txt.fs");

	Offset offset = fsManager.pop();
	assert(0==offset);
	assert(fsManager.isBlockUsed(offset));

	std::cout <<"OK"<< std::endl;
}
